package java44th.group.service.admin.storage;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java44th.group.bean.admin.bo.storage.ListBo;
import java44th.group.bean.common.CommonData;
import java44th.group.bean.po.MarketStorage;
import java44th.group.bean.po.MarketStorageExample;
import java44th.group.mapper.MarketStorageMapper;
import java44th.group.utils.LocalHost;
import java44th.group.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @Aurhor: 帅的一批
 * @CreateAt: 2022/10/12 10:30
 * @Description:
 * @PACKAGE_NAME： java44th.group.service.admin.storage
 */
@Service
public class StoreageServiceImpl implements StoreageService {

    @Autowired
    MarketStorageMapper marketStorageMapper;

    @Override
    @Transactional
    public MarketStorage saveFileAndReturnFileInfo(String finalPath, MultipartFile file) {

        String name = finalPath.substring(finalPath.lastIndexOf("-") + 1);
        String key = finalPath.substring(finalPath.lastIndexOf("\\") + 1);
        String url = "pic/img/" + key;
        try {
            File file1 = new File(finalPath);
            if (!file1.getParentFile().exists()) {
                file1.mkdirs();
            }
            file.transferTo(file1);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        long size = file.getSize();

//        MarketStorageExample marketStorageExample = new MarketStorageExample();
//        marketStorageExample.

        MarketStorage marketStorage = new MarketStorage(
                null,
                key,
                name,
                file.getContentType(),
                (int) size,
                url,
                new Date(),
                new Date(),
                false
        );
       System.out.println(marketStorage);
        marketStorageMapper.insert(marketStorage);

        marketStorage.setUrl(LocalHost.localhost + marketStorage.getUrl());
        return marketStorage;
    }

    @Override
    public CommonData<MarketStorage> storageList(ListBo listBo) {

        PageHelper.startPage(listBo.getPage(), listBo.getLimit());

        MarketStorageExample marketStorageExample = new MarketStorageExample();
        MarketStorageExample.Criteria criteria = marketStorageExample.createCriteria();

        if (!StringUtil.isEmpty(listBo.getName())) {
            criteria.andNameLike("%" + listBo.getName() + "%");
        }

        if (!StringUtil.isEmpty(listBo.getKey())) {
            criteria.andKeyEqualTo(listBo.getKey());
        }

        List<MarketStorage> marketStorages = marketStorageMapper.selectByExample(marketStorageExample);

        for (MarketStorage marketStorage : marketStorages) {

            if(!marketStorage.getUrl().startsWith("http"))
            marketStorage.setUrl(LocalHost.localhost + marketStorage.getUrl());
        }


        PageInfo<MarketStorage> marketStoragePageInfo = new PageInfo<>(marketStorages);

        return CommonData.data(marketStoragePageInfo);
    }

    @Override
    public MarketStorage updateStorage(MarketStorage marketStorage) {
        marketStorage.setUpdateTime(new Date());
        marketStorageMapper.updateByPrimaryKeySelective(marketStorage);


        return marketStorage;
    }

    @Override
    public void deleteStorage(MarketStorage marketStorage) {


        marketStorageMapper.deleteByPrimaryKey(marketStorage.getId());
    }


}
